package cn.zh.stan.activity;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ChagePs extends BaseActivity {
   ImageView chps_back;

   MyApplication  mapp;
   String uid;
   EditText paypass;
   ImageView ppsimg;
   Button pscbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chage_ps);
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        initg();
        }

    public void initg(){
        chps_back= (ImageView) findViewById(R.id.chps_back);
        paypass= (EditText) findViewById(R.id.paypass);
        ppsimg= (ImageView) findViewById(R.id.ppsimg);
        pscbtn= (Button) findViewById(R.id.pscbtn);
        chps_back.setOnClickListener(this);
        pscbtn.setOnClickListener(this);
        paypass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
               if (editable.length()>0){
                   ppsimg.setVisibility(View.VISIBLE);
               }else{
                   ppsimg.setVisibility(View.INVISIBLE);
               }
            }
        });
        ppsimg.setOnClickListener(this);
      }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.chps_back:
                finish();
                break;
            case R.id.ppsimg:
                paypass.getText().clear();
                break;
            case R.id.pscbtn:
                String pass=paypass.getText().toString();
                getPost(pass);
                break;
         }
      }

      public void getPost(String payps){
          System.out.println("------------->支付密码=="+payps);
          OkHttpUtils.post(BaseUrl.SetPayPwd)
                  .params("uid",uid)
                  .params("payPwd",payps)
                  .execute(new StringCallback() {
                      @Override
                      public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                          System.out.println("------------->设置支付密码结果==="+s);
                          try {
                              JSONObject js=new JSONObject(s);
                              boolean State=js.getBoolean("State");
                              if (State){
                                boolean Flag=js.getBoolean("Flag");
                                  String Msg=js.getString("Msg");
                                  ToastUtil.showLong(ChagePs.this,Msg);
                                  finish();
                              }
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                      @Override
                      public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                          super.onError(isFromCache, call, response, e);
                      }
                  });
                  }


        }
